package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;

import com.lb.jwitter.service.TblCTDTKhoiNganhMonHoc;
import com.lb.jwitter.service.TblCTDTNhomMHTuChonMonHoc;
import com.lb.jwitter.service.TblDKMHDotDK;
import com.lb.jwitter.service.TblDKMHDotDKService;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVien;
import com.lb.jwitter.service.TblDKMHTaiKhoanHocVienService;
import com.lb.jwitter.service.TblMenu;
import com.lb.jwitter.service.TblMenuService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblQuyenMenu;
import com.lb.jwitter.service.TblQuyenMenuService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblThongTinHocVienMonHocService;
import com.lb.jwitter.service.TblThongTinMonHoc;
import com.lb.jwitter.service.TblThongTinMonHocService;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.CTDT;
import com.lb.jwitter.util.KetQuaDangKy;
import com.lb.jwitter.util.NhomMonHocTuChon;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class SearchDanhSachMonHocDangKyAction {

	private TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien;
	public TblDKMHTaiKhoanHocVien getTblDKMHTaiKhoanHocVien() {
		return tblDKMHTaiKhoanHocVien;
	}

	public void setTblDKMHTaiKhoanHocVien(
			TblDKMHTaiKhoanHocVien tblDKMHTaiKhoanHocVien) {
		this.tblDKMHTaiKhoanHocVien = tblDKMHTaiKhoanHocVien;
	}

	private String msmh;
	public String getMsmh() {
		return msmh;
	}

	public void setMsmh(String msmh) {
		this.msmh = msmh;
	}

	private List<TblThongTinMonHoc> tblThongTinMonHocs;
	private TblThongTinMonHocService tblThongTinMonHocService;

	public List<TblThongTinMonHoc> getTblThongTinMonHocs() {
		return tblThongTinMonHocs;
	}

	public void setTblThongTinMonHocs(List<TblThongTinMonHoc> tblThongTinMonHocs) {
		this.tblThongTinMonHocs = tblThongTinMonHocs;
	}
	
	private List<KetQuaDangKy> ketQuaDangKys;

	public List<KetQuaDangKy> getKetQuaDangKys() {
		return ketQuaDangKys;
	}

	public void setKetQuaDangKys(List<KetQuaDangKy> ketQuaDangKys) {
		this.ketQuaDangKys = ketQuaDangKys;
	}
	private CTDT ctdt;

	public CTDT getCtdt() {
		return ctdt;
	}

	public void setCtdt(CTDT ctdt) {
		this.ctdt = ctdt;
	}

	@Autowired // Injected by Spring 
	ApplicationContextProvider contextProvider; 
	 	 
	public String execute() throws Exception
	{

		Map session = ActionContext.getContext().getSession();
		tblDKMHTaiKhoanHocVien =  (TblDKMHTaiKhoanHocVien) session.get("taiKhoanHocVien");
	     if( tblDKMHTaiKhoanHocVien == null) return "noprivilege";
	     
	    ApplicationContext context = contextProvider.getApplicationContext(); 
	    
	    tblThongTinMonHocService = (TblThongTinMonHocService)  context.getBean("tbl_ThongTin_MonHocClient");

	    int dotDKId = (Integer) session.get("dotDKId");

    	ctdt =( CTDT ) session.get("CTDT");
    	
		if(tblDKMHTaiKhoanHocVien != null){
			List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
	    	{ 
		        ConditionReport con = new ConditionReport(); 
		        con.setCol("dotDKId.id");
		        con.setValue1(new Long(dotDKId));
		        con.setOperator("eq");
		        cons.add(con);	
		    }
	    	
		    if(msmh != null && !msmh.equals("")){
		    	
			        ConditionReport con = new ConditionReport(); 
			        con.setCol("monHocId.ma");
			        con.setValue1(msmh);
			        con.setOperator("eq");
			        cons.add(con);	
		    	

		    }
		    //msmh rong, lay ra danh sach cac mon hoc thuoc CTDT theo hoc ky
		    else{
		    	
		    	TblThongTinHocVienMonHocService tblThongTinHocVienMonHocService = (TblThongTinHocVienMonHocService) context.getBean("tbl_ThongTin_HocVien_MonHocClient");
				TblDKMHDotDKService tblDKMHDotDKService = (TblDKMHDotDKService) context.getBean("tbl_DKMH_DotDKClient");
			    
			    if(session.get("dotDKId") != null)
			    	dotDKId = (Integer) session.get("dotDKId");
			    
			    TblDKMHDotDK dotDK = tblDKMHDotDKService.gettblDKMHDotDK(new Long(dotDKId));
			    
				if(tblDKMHTaiKhoanHocVien != null){
					List<String> columns = new ArrayList<String>();
					
					//get danh sach mon hoc chua dat
					List<ConditionReport> cons2 = new ArrayList<ConditionReport>();
					{
						ConditionReport con = new ConditionReport();
						con.setCol("hocVienId.id");
						con.setValue1(tblDKMHTaiKhoanHocVien.getHocVienId().getId());
						con.setOperator("eq");
						cons2.add(con);
					}
					{
						ConditionReport con = new ConditionReport();
						
						con.setType("String");
						con.setCol("trangThai");
						con.setValue1("FAIL"); 
						con.setOperator("eq");
						cons2.add(con);
					}
					
					columns.add("monHocId.id");
					List<Object> monHocFails = (List<Object>) tblThongTinHocVienMonHocService.findColumntblThongTinHocVienMonHocByCondition(cons2, columns, 0, 0);

				    String danhSachIdMonHoc = "";
				    for(Object obj : monHocFails){
				    	danhSachIdMonHoc += obj.toString() + ";";
				    }
				    
				    
				    //ds mon hoc theo CTDT hoc ky hien tai
				    
				    int khoaHoc = tblDKMHTaiKhoanHocVien.getHocVienId().getKhoaHoc();
				    int namHienTai = 0;
				    int hocKyTrongNam = 1;
				    //parse khoa hoc
				    try {
					    namHienTai = Integer.parseInt(dotDK.getHocKyId().getNamHoc());
						
					} catch (Exception e) {
					    namHienTai = tblDKMHTaiKhoanHocVien.getHocVienId().getKhoaHoc();
					}

				    //parse hocky trong nam
				    try {
						hocKyTrongNam = Integer.parseInt(dotDK.getHocKyId().getMa().substring(4));
					} catch (Exception e) {
						e.printStackTrace();
					}
				    
				    int hocKyHienTaiTheoCTDT = (namHienTai- khoaHoc)*2 + hocKyTrongNam;

				    //ds mon bat buoc
				    for(TblCTDTKhoiNganhMonHoc monHocBB : ctdt.getMonHocs()){
				    	try {
				    		if(hocKyHienTaiTheoCTDT == Integer.parseInt(monHocBB.getHocKy()))
					    		danhSachIdMonHoc += monHocBB.getMonHocId().getId() +";";
						} catch (Exception e) {
						}
				    }
//				    //ds mon hoc tu chon
//				    for(NhomMonHocTuChon nhom : ctdt.getNhomMonHocTuChons()){
//				    	
//				    	try {
//				    		if(hocKyHienTaiTheoCTDT == Integer.parseInt(nhom.getNhomTuChon().getHocKy())){
//				    			
//				    			for(TblCTDTNhomMHTuChonMonHoc monHocTC : nhom.getMonHocTuChons()){
//						    		danhSachIdMonHoc += monHocTC.getMonHocId().getId() +";";
//				    			}
//				    		}//end-if
//						} catch (Exception e) {
//						}
//				    }

				    
				    { 
				        ConditionReport con = new ConditionReport(); 
				        con.setCol("monHocId.id");
				        con.setType("Long");
				        con.setValue1(danhSachIdMonHoc);
				        con.setOperator("in");
				        cons.add(con);	
				    }
				}else{
					return "noprivilege";
				}
		    }
		    

		    tblThongTinMonHocs = tblThongTinMonHocService.findtblThongTinMonHocByCondition(cons, 0, 0);
		    ketQuaDangKys = (List<KetQuaDangKy>) session.get("ketQuaDangKys");
			
		}
		return Action.SUCCESS;

	}

	
}
